/*
wheel.h
offer wheel.h headfile ,api to outside
designed by lunanting

-------------------------------
2020-03-30 21:35:20
create file
-------------------------------
*/
#ifndef __WHEEL_H
#define __WHEEL_H

#include "panasonic.h"

#define PI 3.1415926

#define WHEEL_line_speed 0.1 //车轮的线速度，单位:m/s
#define AGV_R   0.644   //agv 两轮间距

#define WHEEL_radius 	92	//轮半径，单位mm
#define WHEEL_ratio		21	//轮速比
#define WHEEL_dist		644	//轮间距，单位mm

#ifdef WASHER_MERGE
#define WHEEL_left_addr  12  //左轮电机地址
#define WHEEL_right_addr 13  //左轮电机地址
#else
#define WHEEL_left_addr  10  //左轮电机地址
#define WHEEL_right_addr 11  //左轮电机地址
#endif

#define WHEEL_acc_dec 5000  //加减速

#define WHEEL_left_fw   1     //左轮前进
#define WHEEL_left_bk   0     //左轮后退
#define WHEEL_right_fw  0     //右轮前进
#define WHEEL_right_bk  1     //右轮后退

#ifdef WASHER_MERGE
#define WHEEL_io_left_event 5    //左轮事件io引脚
#define WHEEL_io_right_event 6    //左轮事件io引脚
#define WHEEL_io_left_error 8   //驱动器报错
#define WHEEL_io_right_error 8   //驱动器报错
#else
#define WHEEL_io_left_event 1    //左轮事件io引脚
#define WHEEL_io_right_event 2    //左轮事件io引脚
#define WHEEL_io_left_error 1   //驱动器报错
#define WHEEL_io_right_error 2   //驱动器报错
#endif
#define WHEEL_io_stop 1    //io 触发停机
#define WHEEL_io_run  0     //io 触发启动

/**********************type define***********************/
//wheel turn direction
typedef enum{
    wheel_left, //向左
    wheel_middle,   //中间
    wheel_right,    //向右
}WHEEL_turn;


//wheel lock type
typedef enum{
    wheel_locker_release=0,     //解锁
    wheel_locker_safe,          //避障
    wheel_locker_near,          //距离车辆过近保护，避免撞车
    wheel_locker_wait,          //多机协同等待保护
    wheel_locker_rang_max,      //场地外围保护
    wheel_locker_rang_min,      //场地内围保护

    wheel_locker_max,           //最大设定
}WHEEL_locker_type;

//wheel object
typedef struct{
    u8 init;    //init state
    s8 dir;     //后退<0,停止=0，前进>0
    WHEEL_turn turn;    //turn direction
    int normal_speed;   //额定速度
    int speed[2];   //wheel speed without dir
    u8 dirs[2]; //wheels dir
    int speeds[2];  //wheels speed with dir
    u8 io[2];   //wheel control io pin number
    u8 speed_block[2]; //速度 block number
    float angle;   //行车角度
    PANS_handler *motor[2];    //motor object pointer
    WHEEL_locker_type locker;   //轮锁
}WHEEL_object;


/**********************function extern********************/
extern int xp_wheel_init(u8 port,int speed,u8 slave_id); //wheel motor init
extern int xp_wheel_recover(u8 port,int speed,u8 slave_id); //wheel motor recover
extern int xp_motor_run(u8 run,u8 dir); //wheel motor run
extern int xp_wheel_move(int speed,s8 dir);    //wheels move
extern int xp_wheel_turn(WHEEL_turn turn,int s);  //wheel turn
extern int xp_wheel_round(WHEEL_turn turn,float angle); //wheel round a angle
extern int xp_wheel_motorSpeed_set(int speed0,int speed1);  //set wheel motors speed
extern int xp_wheel_get_speed(float line_speed);    //wheel get motor speed
extern int xp_wheel_get_dir(void);  //get wheel motor dir state
extern WHEEL_object *xp_wheel_object_get(void); //get wheel object
extern int xp_wheel_set_wheel_speed(float line_speed0,float line_speed1);    //set wheels line speed
extern int xp_wheel_stop(void); //set wheels stop
extern int xp_wheel_set_acc(u16 acc,u16 dec,u8 block);      //set motor acc
extern int xp_wheel_set_wheel_locker(int val);
extern int xp_wheel_get_wheel_locker();

extern void xp_agv_wheel_test(void);    //agv test
extern void xp_wheel_test(void);
extern void xp_wheel_cycle(void);    //wheel motor test 
extern void xp_wheel_read_encode(void); //wheel motor read encode
extern void xp_motor_back(u16 run_time);    //wheel motor back
extern void xp_motor_fw(u16 run_time);
extern int xp_wheel_enable_set(bool val);
extern float xp_wheel_diffSpeed_get(float rad,float speed); //get speed different from turn radius and base speed
extern int xp_wheel_get_encode(uint64_t *encodeBuf);
extern int xp_wheel_error_io_check(void);   //check wheel driver error io state
extern int xp_wheel_check_error(void);
extern int xp_clear_wheel_warn(void);

extern const char *xp_lockerString_get(WHEEL_locker_type type);    //get enum type string
extern int xp_wheel_locker_set(WHEEL_locker_type type);    //wheel locker set
extern WHEEL_locker_type xp_wheel_locker_get(void); //wheel locker get
//获取实时轮速,agv在移动状态返回true
extern bool xp_wheel_get_wheel_speed(float * const speed_L, float * const speed_R);
extern void xp_wheel_run_check_notify(void);    //通知检测轮速
extern int xp_wheel_debug(char *type,char *fun,char *param);

#endif
//end of the file
